﻿using System;
using System.Collections.Generic;
using System.Linq;
using Database.Context;
using Database.Models;
using QuanLyThongTinCoSo.Common;
using EntityState = System.Data.Entity.EntityState;

namespace QuanLyThongTinCoSo.Biz
{
    public class VanBanThamDuyetBiz:BaseBiz
    {
        public VanBanThamDuyetBiz()
        {
            DbContext = new Context();
        }

        public VanBanThamDuyetBiz(Context context)
        {
            DbContext = context;
        }

        public override void Insert(BaseModel model)
        {
            var m = model as VanBanThamDuyetPCCC;
            m.Created = DateTime.Now;
            m.Modified = DateTime.Now;
            m.CreatedBy = Utilities.CurrentUser();
            m.ModifiedBy = Utilities.CurrentUser();

            DbContext.VanBanThamDuyets.Add(m);
            DbContext.SaveChanges();
        }

        public override void Update(BaseModel model)
        {
            var m = model as VanBanThamDuyetPCCC;
            m.Modified = DateTime.Now;
            m.ModifiedBy = Utilities.CurrentUser();

            DbContext.Entry(m).State = EntityState.Modified;
            DbContext.SaveChanges();
        }

        public override void Delete(BaseModel model)
        {
            DbContext.VanBanThamDuyets.Attach(model as VanBanThamDuyetPCCC);
            DbContext.VanBanThamDuyets.Remove(model as VanBanThamDuyetPCCC);
            DbContext.Entry(model).State = EntityState.Deleted;
            DbContext.SaveChanges();
        }

        public override void DeleteItems(List<BaseModel> models)
        {
            throw new NotImplementedException();
        }

        public override BaseModel GetItemById(int id)
        {
            return (from item in DbContext.VanBanThamDuyets
                    where item.Id == id
                    select item).First();
        }

        public List<VanBanThamDuyetPCCC> GetAll()
        {
            return DbContext.VanBanThamDuyets.OrderBy(c => c.Ten).ToList();
        }
    }
}
